<?php

namespace STAGE\StageBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use STAGE\StageBundle\Entity\Societe;
use STAGE\StageBundle\Form\SocieteType;
use STAGE\UserBundle\Entity\user;
use Symfony\Component\HttpFoundation\Response;

class SocieteController extends Controller {

    public function indexAction() {
        return $this->render('StageBundle:Societe:index.html.twig');
    }

    public function newAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();
        $id = $request->get('id');
        $idresp = $request->get('idresp');
        
        if ($id == '')
            $societe = new Societe();
        else
            $societe = $em->getRepository('StageBundle:Societe')->find($id);
        $form = $this->createForm(new SocieteType(), $societe);
        if ($request->isMethod('post')) {
            $form->bind($request);
//            if($form->isValid()){
            $societe = $form->getData();
            $em->persist($societe);
            $em->flush();
            $url = $this->generateUrl('stage_societe_list');
            return $this->redirect($url);
//            }
        }
       
        return $this->render('StageBundle:Societe:new.html.twig', array('id' => $id,'idresp' => $idresp, 'form' => $form->createView()));
    }

    public function listAction() {
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $type = $request->get('type');
        $societes = $em->getRepository('StageBundle:Societe')->findAll();
        if ($type != '') {
            return $this->render('StageBundle:Societe:page_list.html.twig', array('societes' => $societes));
        }
        return $this->render('StageBundle:Societe:list.html.twig', array('societes' => $societes));
    }

    public function deleteAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();
        $id = $request->get('id');
        $societe = $em->getRepository('StageBundle:Societe')->find($id);
        $em->remove($societe);
        $em->flush();
        $url = $this->generateUrl('stage_societe_list', array('type' => 'ajax'));
        return $this->redirect($url);
    }

    public function listrespAction() {
        $em = $this->getDoctrine()->getManager();
        $request = $this->getRequest();
        $type = $request->get('type');
        $encadreurs = $em->getRepository('UserBundle:User')->getEncadreur();
        if ($type != '') {
            $societe=$encadreurs->getSociete().getId();
            return $this->render('StageBundle:Societe:page_list_encadreur.html.twig', array('encadreurs' => $encadreurs));
        }
        return $this->render('StageBundle:Societe:list_encadreur.html.twig', array('encadreurs' => $encadreurs));
    }

    public function deleterespAction() {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();
        $id = $request->get('id');
        $encadreur = $em->getRepository('UserBundle:User')->find($id);
        $em->remove($encadreur);
        $em->flush();
        $url = $this->generateUrl('stage_societe_list_responsable', array('type' => 'ajax'));
        return $this->redirect($url);
    }

}
